﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BugTracker.Models;
using System.Data;
using System.Data.Entity;
using BugTracker.Controllers;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
//using System.Data.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Text;
using System.IO;
using System.Data.Objects;
using System.Data.Entity.Infrastructure;
using BugTracker.Helper;

namespace BugTracker.Controllers
{
    public class QAController : Controller
    {

        Bug_db db = new Bug_db();
        SessionHelper sessionhelper = new SessionHelper();
        // Home Dashboard
        public ActionResult Home()
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();

            if (sessionhelper.Session_Info() != null)
            {
                if (sessionhelper.SessionAuth(sessionhelper.Session_Info().session_idPK) == false)
                {
                    return RedirectToAction("Login", "Home", new { msg = "expired" });
                }
                else
                {
                    return View();
                }
            }
            return RedirectToAction("Login", "Home", new { msg = "expired" });
        }

        //View Project Table

        public ActionResult Project()
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();

            if (sessionhelper.Session_Info() != null)
            {
                if (sessionhelper.SessionAuth(sessionhelper.Session_Info().session_idPK) == false)
                {
                    return RedirectToAction("Login", "Home", new { msg = "expired" });
                }
                else
                {
                    return View(db.Projects.ToList());
                }
            }
            return RedirectToAction("Login", "Home", new { msg = "expired" });
        }

        // View Project Details
        public ActionResult DetailsProject(int id)
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();

            if (sessionhelper.Session_Info() != null)
            {
                if (sessionhelper.SessionAuth(sessionhelper.Session_Info().session_idPK) == false)
                {
                    return RedirectToAction("Login", "Home", new { msg = "expired" });
                }
                else
                {
                    Project project = db.Projects.Find(id);
                    return View(project);
                }
            }
            return RedirectToAction("Login", "Home", new { msg = "expired" });
        }

        //View Issue Table
        public ActionResult Issue()
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();

            if (sessionhelper.Session_Info() != null)
            {
                if (sessionhelper.SessionAuth(sessionhelper.Session_Info().session_idPK) == false)
                {
                    return RedirectToAction("Login", "Home", new { msg = "expired" });
                }
                else
                {
                    return View(db.Issues.ToList());
                }
            }
            return RedirectToAction("Login", "Home", new { msg = "expired" });
        }

        //Create New Issue
        public ActionResult CreateIssue()
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();

            if (sessionhelper.Session_Info() != null)
            {
                if (sessionhelper.SessionAuth(sessionhelper.Session_Info().session_idPK) == false)
                {
                    return RedirectToAction("Login", "Home", new { msg = "expired" });
                }
                else
                {
                    ViewData["ProjectName"] = (db.Projects.OrderBy(p => p.project_idPK)).ToList();
                    //ViewData["ProjectAssign"] = (db.Projects.OrderBy(p => p.user_idFK)).ToList();
                    ViewData["Developer"] = db.Users.Where(d => d.role_idFK == 2).ToList();
                    return View();
                }
            }
            return RedirectToAction("Login", "Home", new { msg = "expired" });
        }

        //Save new created Issue
        [HttpPost]
        public ActionResult CreateIssue(Issue issue)
        {
            if (ModelState.IsValid)
            {

                issue.date_identified = System.DateTime.Now.ToString();
                db.Issues.Add(issue);
                db.SaveChanges();
                return RedirectToAction("Issue");
            }
          
            return View(issue);
        }

        // View Issue Details 
        public ActionResult DetailsIssue(int id)
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();

            if (sessionhelper.Session_Info() != null)
            {
                if (sessionhelper.SessionAuth(sessionhelper.Session_Info().session_idPK) == false)
                {
                    return RedirectToAction("Login", "Home", new { msg = "expired" });
                }
                else
                {
                    Issue issue = db.Issues.Find(id);
                    return View(issue);
                }
            }
            return RedirectToAction("Login", "Home", new { msg = "expired" });
        }

        //Edit issue (only status can be edited)
        public ActionResult EditIssue(int id)
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();

            if (sessionhelper.Session_Info() != null)
            {
                if (sessionhelper.SessionAuth(sessionhelper.Session_Info().session_idPK) == false)
                {
                    return RedirectToAction("Login", "Home", new { msg = "expired" });
                }
                else
                {
                    //ViewData["ProjectName"] = (db.Projects.OrderBy(p => p.project_idPK)).ToList();
                    ViewData["ProjectName"] = (db.Projects.OrderBy(p => p.project_idPK)).ToList();

                    Issue issue = db.Issues.Find(id);
                    ViewBag.dateidentified = issue.date_identified;
                    return View(issue);
                }
            }
            return RedirectToAction("Login", "Home", new { msg = "expired" });
        }


        //Save edited Issue 
        [HttpPost]
        public ActionResult EditIssue(Issue issue)
        {
          
            if (ModelState.IsValid)
            {
                db.Entry(issue).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Issue");
            }
            return View(issue);
        }

        //Delete Issue
        public ActionResult DeleteIssue(int id)
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.UtcNow.AddHours(-1));
            Response.Cache.SetNoStore();

            if (sessionhelper.Session_Info() != null)
            {
                if (sessionhelper.SessionAuth(sessionhelper.Session_Info().session_idPK) == false)
                {
                    return RedirectToAction("Login", "Home", new { msg = "expired" });
                }
                else
                {
                    Issue issue = db.Issues.Find(id);
                    return View(issue);
                }
            }
            return RedirectToAction("Login", "Home", new { msg = "expired" });
        }

        //Delete Issue
        [HttpPost, ActionName("DeleteIssue")]
        public ActionResult DeleteIssueConfirmed(int id)
        {
            Issue issue = db.Issues.Find(id);
            db.Issues.Remove(issue);
            db.SaveChanges();
            return RedirectToAction("Issue");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}
    


